package com.db;

import com.db.DbConnection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class SharePath {
	public void sharePath(int userid, int pathid) {
		String sql = "insert into route_share(rid,uid) values ('" + pathid
				+ "','" + userid + "')";

		Connection con = null;

		try {
			con = DbConnection.getInstance().getConnection();

			Statement st = con.createStatement();

			st.executeUpdate(sql);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	public void deleteShare(int userid, int pathid) {
		String sql = "delete from route_share where rid='" + pathid
				+ "' and uid='" + userid + "'";

		Connection con = null;

		try {
			con = DbConnection.getInstance().getConnection();

			Statement st = con.createStatement();

			st.executeUpdate(sql);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	public ArrayList showSharePath(int userid){
		ArrayList<Map> paths=new ArrayList<Map>();
		String sql = "select * from route_share where uid='"+userid+"'";
		Connection con = null;
		
		
		try {
			con = DbConnection.getInstance().getConnection();
			Statement st = con.createStatement();
			Statement st1 = con.createStatement();
			ResultSet rs = st.executeQuery(sql);
			Map<String, Object> path = null;
			ResultSet temp=null;
			while (rs.next()) {
				path=new HashMap<String, Object>();
				path.put("pathid", rs.getString("rid"));
				sql="select * from all_path where id="+rs.getString("rid");
				temp = st1.executeQuery(sql);
				temp.next();
				path.put("pathname", temp.getString("pathname"));
				sql="select * from user where id="+temp.getString("userid");
				
				temp = st1.executeQuery(sql);
				temp.next();
				path.put("username", temp.getString("username"));
				
				paths.add(path);

			}
		} catch (SQLException e) {
			paths = null;
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return paths;
	}
}
